package com.raincat.tools.multimediapickertools.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.raincat.tools.R;


/**
 * <pre>
 *     author : RainCat
 *     org    : Shenzhen JingYu Network Technology Co., Ltd.
 *     e-mail : nining377@gmail.com
 *     time   : 2019/02/15
 *     desc   : 标题栏
 *     version: 1.0
 * </pre>
 */

public class EasyActionBar extends RelativeLayout {
    private Context context;

    private ImageView action_back;
    private TextView action_title;
    private ImageView action_select;
    private RelativeLayout action_rightLayout;

    public EasyActionBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
        this.context = context;
    }

    public EasyActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(context, attrs);
    }

    public EasyActionBar(Context context) {
        super(context);
        this.context = context;
        init(context, null);
    }

    private void init(final Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.picker_view_easyactionbar, this);
        action_back = findViewById(R.id.action_back);
        action_title = findViewById(R.id.action_title);
        action_select = findViewById(R.id.action_select);
        action_rightLayout = findViewById(R.id.action_rightLayout);

        action_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Activity) context).finish();
            }
        });
    }

    public void setImmerseLayout() {
        int height = getStatusBarHeight();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setPadding(0, height, 0, 0);
        }
    }

    /**
     * 用于获取状态栏的高度。 使用Resource对象获取（推荐这种方式）
     *
     * @return 返回状态栏高度的像素值。
     */
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public void setTitleRes(int resId) {
        if (resId != -1) {
            Drawable drawable = getResources().getDrawable(resId);
            action_title.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
        } else
            action_title.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
    }

    public void setTitleText(String text) {
        action_title.setText(text);
    }

    public void setTitleTextColor(int paramInt) {
        action_title.setTextColor(paramInt);
    }

    public void setOnTitleClickListener(OnClickListener paramOnClickListener) {
        action_title.setOnClickListener(paramOnClickListener);
    }

    public void setRightLayoutVisibility(int visibility) {
        action_rightLayout.setVisibility(visibility);
    }

    public void setRightImageRes(boolean isSelect) {
        if (isSelect) {
            action_select.setBackgroundResource(R.drawable.picker_check_enable);
            action_select.setImageResource(R.mipmap.picker_check);
        } else {
            action_select.setBackgroundResource(R.drawable.picker_check_disable);
            action_select.setImageDrawable(null);
        }
    }

    public void setLeftImageRes(int paramInt) {
        action_back.setImageResource(paramInt);
    }

    public void setOnRightLayoutClickListener(OnClickListener paramOnClickListener) {
        action_rightLayout.setOnClickListener(paramOnClickListener);
    }

    public void setOnLeftLayoutClickListener(OnClickListener paramOnClickListener) {
        action_back.setOnClickListener(paramOnClickListener);
    }
}
